<?php
class PhotoPath{
	private $_url = '';
	private $_filename = '';
	private $_folder;
	private $_ext = '.png';
	private $_datePath;
	function __construct($url){
		$this->_url = $url;
		//$this->_ext = 'png';
		$this->_folder = 'urlthumb';
		$this->_datePath = date("Y"). DS .date("m"). DS.date("d") . DS;
		$this->_savePath = $this->getPath();
		$this->_saveName = md5($this->_url);
		if(!is_dir($this->_savePath)){
			@mkdir($this->_savePath,0777,1);
		}
	}
	function randomkeys($length){
        	$key='';
        	$pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
   			for($i=0;$i<$length;$i++){
   				$key .= $pattern{rand(0,35)};
   			}
   			return $key;
  	   }

	public function getPath(){
		return dirname(__FILE__).DS.'..'.DS.'..'.DS.'..'.DS.'..'.DS.'public'.DS.$this->_folder.DS .$this->_datePath;
	}
	public function getWebPath(){
		return $this->_folder . DS .$this->_datePath. $this->_saveName . $this->_ext;
	}
	public function getSavePath(){
		return $this->_savePath . $this->_saveName . $this->_ext;
	}
	public function savePic($response){
		$filename = $this->getSavePath();
		if(is_file($filename)){
			return true; 
		}else{
			$this->fp = fopen($filename,'w');
			fwrite($this->fp,$response);
			fclose($this->fp);
		}
	}
}